#!/usr/bin/env python

import mechanize

class Browser(mechanize.Browser):
    """Facebook browser"""
    
    STATUS_LOGGED = 'Logged'
    STATUS_ACCOUNT_LOCKED = 'Your account is temporarily locked'
    STATUS_ACCOUNT_DISABLED = 'Your account has been disabled'
    STATUS_WRONG_PASSWORD = 'The password you entered is incorrect'
    STATUS_NON_USER = "We didn't recognize your email address"
    STATUS_TRY_AGAIN = 'Please try again later'
    STATUS_SECURITY_CHECK = 'Security Check'
    STATUS_PLEASE_CONFIRM = ('Please enter the words to confirm your account '
                             'with Facebook')
    STATUS_UNKNOWN = 'Unknown'
    
    def __init__(self, email, password, debug=False):
        mechanize.Browser.__init__(self)
        
        self.email = email
        self.password = password
        self._debug = debug
        
        self.set_handle_robots(False)
        self._cookies = mechanize.CookieJar()
        userAgent = ('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) '
                     'Gecko/20100101 Firefox/23.0')
        self.addheaders = [('User-agent', userAgent)]
        
    _response = None
    
    def read(self):
        """Read submit response"""
        
        if self._debug:
            self._response = 'Edit Profile'
        
        if self._response: return self._response
        
        self.open("https://www.facebook.com")
        self.select_form(nr=0)
        self.form['email'] = self.email
        self.form['pass'] = self.password
        self._response = self.submit().read()
        return self._response
    
    _status = None
    
    @property
    def status(self):
        """Status of username and password"""
        
        if self._status: return self._status
        
        if 'Your account has been disabled.' in self._response:
            self._status = self.STATUS_ACCOUNT_DISABLED
        if 'Akun Anda telah dinonaktifkan.' in self._response:
            self._status = self.STATUS_ACCOUNT_DISABLED
            
        elif "recognise your email address." in self._response:
            self._status = self.STATUS_NON_USER
        elif 'Incorrect Email' in self._response:
            self._status = self.STATUS_NON_USER
        elif 'Incorrect email' in self._response:
            self._status = self.STATUS_NON_USER
        elif "Kami tidak mengenali alamat email Anda." in self._response:
            self._status = self.STATUS_NON_USER
            
        elif 'The password you entered is incorrect.' in self._response:
            self._status = self.STATUS_WRONG_PASSWORD
        elif 'Kata sandi yang Anda masukkan salah.' in self._response:
            self._status = self.STATUS_WRONG_PASSWORD
        elif 'Sorry! You entered an old password' in self._response:
            self._status = self.STATUS_WRONG_PASSWORD
            
        elif 'Your account is temporarily locked.' in self._response:
            self._status = self.STATUS_ACCOUNT_LOCKED
            
        elif 'to confirm your account with Facebook.' in self._response:
            self._status = self.STATUS_PLEASE_CONFIRM
        elif 'untuk mengonfirmasi akun Anda di Facebook.' in self._response:
            self._status = self.STATUS_PLEASE_CONFIRM
            
        elif 'Please try again later' in self._response:
            self._status = self.STATUS_TRY_AGAIN
        elif 'Coba lagi nanti' in self._response:
            self._status = self.STATUS_TRY_AGAIN
            
        elif 'Security Check' in self._response:
            self._status = self.STATUS_SECURITY_CHECK
        elif 'Pemeriksaan Keamanan' in self._response:
            self._status = self.STATUS_SECURITY_CHECK
            
        elif 'Edit Profile' in self._response:
            self._status = self.STATUS_LOGGED
        elif 'Sunting Profil' in self._response:
            self._status = self.STATUS_LOGGED
        elif 'Logout' in self._response:
            self._status = self.STATUS_LOGGED
        elif 'Log out' in self._response:
            self._status = self.STATUS_LOGGED
            
        else:
            self._status = self.STATUS_UNKNOWN
            
        return self._status
            
if __name__ == '__main__':
    pass
